package com.train.system.mapper;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.train.common.model.page.PageSorted;
import com.train.common.resp.PageResp;
import com.train.system.dto.station.StationDTO;
import com.train.system.resp.station.StationPageResp;
import com.train.system.resp.station.StationResp;
import com.train.system.dto.station.StationPageDTO;
import com.train.system.po.StationPO;
import com.train.mybatis.base.MyBaseMapper;

import java.util.List;

/**
* @author baitiaojun
* @description 针对表【station(乘车人)】的数据库操作Mapper
* @createDate 2025-02-14 19:37:18
* @Entity generator.domain.Station
*/
public interface StationMapper extends MyBaseMapper<StationPO> {

    default PageResp<StationPageResp> pageList(StationPageDTO stationPageDTO) {
        PageResp<StationPO> stationPOPageResp = this.selectPage(
                stationPageDTO,
                new LambdaQueryWrapper<>(), new PageSorted(StationPO::getUpdateTime), new PageSorted(StationPO::getCreateTime));
        List<StationPageResp> stationPageRespList = BeanUtil.copyToList(stationPOPageResp.getData(), StationPageResp.class);
        return new PageResp<>(stationPOPageResp.getTotal(), stationPageRespList);
    }

    default boolean existsId(Long id) {
        LambdaQueryWrapper<StationPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StationPO::getId, id);
        return this.exists(queryWrapper);
    }

    default List<StationResp> selectAllStation() {
        LambdaQueryWrapper<StationPO> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(StationPO::getName)
                .orderBy(true, true,  StationPO::getNamePinyin);
        List<StationPO> stationPOList = selectList(wrapper);
        return BeanUtil.copyToList(stationPOList, StationResp.class);
    }

    default boolean exists(String name) {
        LambdaQueryWrapper<StationPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StationPO::getName, name);
        return this.exists(queryWrapper);
    }

    default StationDTO getId(String name) {
        LambdaQueryWrapper<StationPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(StationPO::getId).eq(StationPO::getName, name);
        StationPO stationPO = this.selectOne(queryWrapper);
        return BeanUtil.copyProperties(stationPO, StationDTO.class);
    }
}